package com.dp.util;

import java.util.HashMap;
import java.util.Properties;

import com.dp.service.impl3.KeyInfo;



/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright:
 * <p>Company: </p>
 * @author xiaobin
 * @version 1.0
 */
public class KeyGenerator {
    private static KeyGenerator keygen = new KeyGenerator();
    private static final int POOL_SIZE = 5;
    static Properties pageProperties = null;
    private HashMap keyList = new HashMap(10);

    private KeyGenerator() {
    }

    /**
    *��̬�����������ṩ�Լ���ʵ��
    */
    public static KeyGenerator getInstance() {
        return keygen;
    }

    /**
    *ȡֵ�������ṩ��һ����ʵļ�ֵ
    */
    public synchronized long getNextKey(String keyName) {
        KeyInfo keyinfo;
        long strKey;

        if (keyList.containsKey(keyName)) {
            keyinfo = (KeyInfo) keyList.get(keyName);
        } else {
            keyinfo = new KeyInfo(POOL_SIZE, keyName);
            keyList.put(keyName, keyinfo);
        }

        strKey = keyinfo.getNextKey();

        return strKey;
    }

    public String getNextKeyValue(String tableName) {
        String strKey = "";
        String strRule = "";

        try {
            if (pageProperties == null) {
                MessagePropertise msgPropertise = new MessagePropertise();
                pageProperties = msgPropertise.LoadPropertie("db_key_rule.properties");
            }

            strRule = pageProperties.getProperty(tableName);            
            strKey = strRule + getNextKey(tableName);
        } catch (Exception e) {
            e.getMessage();
        }

        return strKey;
    }
}
